package chapter06

/**
 * 构造器参数
 */
object Demo06_ConstructorParam {
  def main(args: Array[String]): Unit = {
    val student2 = new Student2
    student2.name = "alice"
    student2.age = 18
    println(s"student2: name = ${student2.name}, age = ${student2.age}")

    val student3 = new Student3("bob",55)
    println(s"student3: name = ${student3.name}, age = ${student3.age}")

    val student4 = new Student4("cary",75)
    student4.printInfo()

    val student5 = new Student5("nike",85)
    println(s"student5: name = ${student5.name}, age = ${student5.age}")

    val student6 = new Student6("cary",75)
    println(s"student6: name = ${student6.name} , age = ${student6.age} ")
    student6.printInfo()
  }
}
//定义类
//无参构造器
class Student2 {
  //单独定义属性
  var name: String = _
  var age: Int = _
}
  //上面定义等价于
//直接给主构造器传参，直接定义出它对应的属性
class Student3(var name: String,var age: Int)

//主构造器参数无修饰
class Student4( name: String, age: Int){
  //此时属性里面就米有  只是一个形参
  def printInfo(): Unit ={
    println(s"Student4: name = $name, age = $age")
  }
}
//使用val修饰后  不能在进行增长了
class Student5(val name: String , val age: Int)

//
class Student6(var name: String , var age: Int){
  var school:String = _
  def this(name: String, age: Int , school: String){
    this(name,age)
    this.school= school
  }
  def printInfo(): Unit ={
    println(s"Student6: name = $name, age = $age , school= $school")
  }
}

